package com.heima.test;

import com.heima.mapper.BrandMapper;
import com.heima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

public class delete {
    /**
     * 删除一个数据和批量删除数据
     * @throws Exception
     */
    @Test
    public void deleteById() throws Exception {
        int id=3;
        int status=0;
        String companyName="苹果";
        String brandName="苹果";
        int ordered=200;
        String description="苹果手机很好用";


//        companyName="%"+companyName+"%";
//        brandName="%"+brandName+"%";

        //创建Brand对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //设置mybatis自动提交事务
//        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //方式一 ：接口方法参数使用 @Param 方式调用的方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //方式二 ：接口方法参数是 实体类对象 方式调用的方法
        //封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setId(id);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        brand.setOrdered(ordered);
//        brand.setDescription(description);
//        mapper.deleteById(8);
        int []ids={5,6,7};
        mapper.deleteByIds(ids);


        sqlSession.close();
    }

}
